package com.stone.delay.api.v1.controller;

import com.stone.delay.app.queue.RedisDelayQueueManager;
import com.stone.delay.domain.entity.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController("redisDelayQueueController.v1")
@RequestMapping("/v1/delayed-queue")
public class RedisDelayedQueueController {

    @PostMapping
    public ResponseEntity<?> add(@RequestParam("queue") String queue, @RequestBody Order data) {
//        RedisDelayQueueManager.add(queue, data);
        RedisDelayQueueManager.offer(queue, data, 15, TimeUnit.SECONDS);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/add-with-time")
    public ResponseEntity<?> addWithTime(@RequestParam("queue") String queue,
                                         @RequestParam("delayed") Integer delayed,
                                         @RequestBody Order data) {
        RedisDelayQueueManager.offer(queue, data, delayed, TimeUnit.SECONDS);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/add-with-time1")
    public ResponseEntity<?> addWithTime1(@RequestParam("queue") String queue,
                                          @RequestParam("delayed") Integer delayed,
                                          @RequestBody Order data) {
        RedisDelayQueueManager.offer(queue, data, delayed, TimeUnit.SECONDS);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping
    public ResponseEntity<?> remove(@RequestParam("queue") String queue, @RequestBody Order data) {
        RedisDelayQueueManager.remove(queue, data);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}